<?php
namespace app\api\controller;
use think\Controller;
use think\Db;

class User extends Common {
  // 用户注册接口
  public function register() {
    /*********** 接收参数  ***********/
    $data = $this->params;
    /*********** 检测用户名  ***********/
    $this->check_exist($data['user_name'], 0);

    $data['user_name'] = $data['user_name'];
    $data['user_password'] = $data['password'];
    /*********** 将用户信息写入数据库  ***********/
    $res = db('users')->insert($data);
    if (!$res) {
        $this->return_msg(400, '用户注册失败!');
    } else {
        $this->return_msg(200, '用户注册成功!');
    }
  }

  // 用户登陆
  public function login() {
    /*********** 接收参数  ***********/
    $data = $this->params;
   
    $res = $this->check_exist($data['user_name'], 1);
    $db_res = db('users')
        ->field('id,user_name,user_password')
        ->where('user_name', $data['user_name'])
        ->find();
    if ($db_res['user_password'] !== md5($data['password'])) {
        $this->return_msg(400, '用户名或者密码不正确!');
    } else {
        unset($db_res['user_password']); // 密码永不返回
        $this->return_msg(200, '登录成功!', $db_res);
    }
  }

}